package com.example.demo.controller;

import com.example.demo.annotation.IgnoreAuth;
import com.example.demo.utils.AliOSSUtils;
import com.example.demo.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/oss")
public class OssController {
    @Autowired
    private AliOSSUtils aliOSSUtils;
    @IgnoreAuth
    @PostMapping("/upload")
    public R upload(@RequestParam MultipartFile file) throws IOException {
        System.out.println("执行了");
        //保存到阿里云oss
//        try {
//            if(file.isEmpty()){
//                return R.error("file is empty");
//            }
//            String url = aliOSSUtils.upload(file);
//            Map<String,Object> map = new HashMap<>();
//            map.put("data",url);
//            map.put("msg","上传成功");
//            map.put("code",200);
//            return R.ok(map);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return R.error("file is empty");
//        }
        if(file.isEmpty()){
            return R.error("file is empty");
        }
        // 上传文件/图像到指定文件夹（这里可以改成你想存放地址的相对路径）
        String path = System.getProperty("user.dir") + "\\upload";
        File savePos = new File(path);
        if(!savePos.exists()){  // 不存在，则创建该文件夹
            savePos.mkdir();
        }
        // 获取存放位置的规范路径
        String realPath = null;
        try {
            realPath = savePos.getCanonicalPath();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        // 上传该文件/图像至该文件夹下
        file.transferTo(new File(realPath+"/"+System.currentTimeMillis()/1000+file.getOriginalFilename()));
        Map<String,Object> map = new HashMap<>();
            map.put("data","upload/"+System.currentTimeMillis()/1000+file.getOriginalFilename());
            map.put("msg","上传成功");
            map.put("code",200);
            return R.ok(map);
    }
}
